package cn.aitrox.ry.service.anno.service.impl;

import cn.aitrox.ry.bean.RespResult;
import cn.aitrox.ry.common.interceptor.UserThreadLocal;
import cn.aitrox.ry.common.service.BaseService;
import cn.aitrox.ry.enumtype.RespCodeEnum;
import cn.aitrox.ry.service.anno.dao.AnnoComponentResultDao;
import cn.aitrox.ry.service.anno.dto.anno.component.result.AcrDto;
import cn.aitrox.ry.service.anno.dto.anno.component.result.out.AcrCreateOutDto;
import cn.aitrox.ry.service.anno.entity.AnnoComponentResultEntity;
import cn.aitrox.ry.service.anno.entity.AnnoResultEntity;
import cn.aitrox.ry.service.anno.service.AnnoComponentResultService;
import cn.aitrox.ry.service.anno.service.AnnoResultService;
import cn.aitrox.ry.util.ArrayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class AnnoComponentResultServiceImpl extends BaseService implements AnnoComponentResultService {

    @Autowired
    private AnnoComponentResultDao componentResultDao;

    @Autowired
    private UserThreadLocal userThreadLocal;

    @Autowired
    private AnnoResultService annoResultService;

    @Transactional
    @Override
    public RespResult<AcrCreateOutDto> batchCreate(Integer arId, Integer alrId, List<AcrDto> acrList) {
        RespResult<AnnoResultEntity> checkDataUserResult = annoResultService.checkAr(userThreadLocal.getUser().getId(), arId);
        if (!checkDataUserResult._isOk()) {
            return RespResult.secByError(checkDataUserResult.getCode(), checkDataUserResult.getMsg());
        }

        final Integer finalAlrId = null == alrId ? 0 : alrId;

        List<AnnoComponentResultEntity> list = acrList.stream().map(x -> {
            return new AnnoComponentResultEntity(null, checkDataUserResult.getData().getTaskId(), arId, finalAlrId, x.getComponentId(), x.getResult());
        }).collect(Collectors.toList());

        if (!ArrayUtil.isEmpty(list)) {
            componentResultDao.batchSave(list);
        }
        List<AnnoComponentResultEntity> acrListInDB = componentResultDao.findByArIdAlrId(arId, alrId);

        List<Integer> componentIdList = acrList.stream().map(AcrDto::getComponentId).collect(Collectors.toList());

        List<AcrDto> outList = acrListInDB.stream().filter(x -> componentIdList.contains(x.getComponentId())).map(x -> {
            return new AcrDto(x.getId(), x.getComponentId(), x.getResult());
        }).collect(Collectors.toList());

        return RespResult.ok(new AcrCreateOutDto(alrId, outList));
    }

    @Transactional
    @Override
    public RespResult update(Integer acrId, String result) {
        RespResult<AnnoComponentResultEntity> checkResult = this.checkAcr(userThreadLocal.getUser().getId(), acrId);
        if (!checkResult._isOk()) {
            return RespResult.secByError(checkResult.getCode(), checkResult.getMsg());
        }
        AnnoComponentResultEntity update = new AnnoComponentResultEntity(acrId, null, null, null, null, result);
        componentResultDao.updateByIdSelective(update);
        return RespResult.ok();
    }


    private RespResult<AnnoComponentResultEntity> checkAcr(Integer userId, Integer acrId) {
        AnnoComponentResultEntity acr = componentResultDao.findById(acrId);
        if (null == acr) {
            return RespResult.error(RespCodeEnum.PARAM_ERROR);
        }
        RespResult<AnnoResultEntity> checkDataUserResult = annoResultService.checkAr(userId, acr.getArId());
        if (!checkDataUserResult._isOk()) {
            return RespResult.secByError(checkDataUserResult.getCode(), checkDataUserResult.getMsg());
        }
        return RespResult.ok(acr);
    }
}
